﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;

namespace ConsoleApplication1
{
    class Program
    {
        private static string m_strUrl = "http://127.0.0.1:8080/";
        private static string m_strUserName = "admin";
        private static string m_strPassword = "admin";

        static void Main(string[] args)
        {
            Console.WriteLine("Xml_Get_Config");
            Console.WriteLine(Xml_Config_Get("sqlalchemy.dburi"));
            Console.WriteLine("Json_Get_Config");
            Console.WriteLine(Json_Config_Get("sqlalchemy.dburi"));
            Console.WriteLine("Xml_DBUri_Get");
            Console.WriteLine(Xml_DBUri_Get());
            Console.WriteLine("Json_DBUri_Get");
            Console.WriteLine(Json_DBUri_Get());

            Console.Read();
        }

        private static string Xml_Config_Get(string strVarName)
        {
            string strParameter =
                "<parameters><varname>"
                + strVarName
                + "</varname></parameters>";

            try
            {
                Uri address = new Uri(m_strUrl + "/api/config/get");
                System.Net.HttpWebRequest request =
                    (HttpWebRequest)System.Net.WebRequest.Create(address);

                request.Accept = "text/xml";
                request.ContentType = "text/xml";
                request.Headers.Add("Content-Encoding", "utf-8");

                Encoding asciiEncoding = Encoding.ASCII;

                byte[] byteArray = asciiEncoding.GetBytes(
                    m_strUserName + ":" + m_strPassword);

                request.Headers.Add(
                    HttpRequestHeader.Authorization,
                    "Basic " + Convert.ToBase64String(byteArray));

                request.Method = "POST";
                request.ContentLength = Encoding.UTF8.GetByteCount(
                    strParameter);
                request.GetRequestStream().Write(
                    Encoding.UTF8.GetBytes(strParameter),
                    0,
                    Encoding.UTF8.GetByteCount(strParameter));

                using (System.Net.WebResponse response
                        = request.GetResponse())
                {
                    System.IO.StreamReader sr =
                        new System.IO.StreamReader(
                            response.GetResponseStream());
                    return sr.ReadToEnd();
                }
            }
            catch (Exception ex)
            {
                return "";
            }
        }

        private static string Xml_DBUri_Get()
        {
            string strParameter = "<parameters />";
            try
            {
                Uri address = new Uri(m_strUrl + "/api/config/dburi");
                System.Net.HttpWebRequest request =
                    (HttpWebRequest)System.Net.WebRequest.Create(address);

                request.Accept = "text/xml";
                request.ContentType = "text/xml";
                request.Headers.Add("Content-Encoding", "utf-8");

                Encoding asciiEncoding = Encoding.ASCII;
                byte[] byteArray = asciiEncoding.GetBytes(
                    m_strUserName + ":" + m_strPassword);
                request.Headers.Add(
                    HttpRequestHeader.Authorization,
                    "Basic " + Convert.ToBase64String(byteArray));

                request.Method = "POST";
                request.ContentLength = Encoding.UTF8.GetByteCount(
                    strParameter);
                request.GetRequestStream().Write(
                    Encoding.UTF8.GetBytes(strParameter),
                    0,
                    Encoding.UTF8.GetByteCount(strParameter));

                using (System.Net.WebResponse response =
                    request.GetResponse())
                {
                    System.IO.StreamReader sr =
                        new System.IO.StreamReader(
                            response.GetResponseStream());
                    return sr.ReadToEnd();
                }
            }
            catch (Exception ex)
            {
                return "";
            }
        }

        private static string Json_Config_Get(string strVarName)
        {
            string strParameter =
                "{ \"varname\" : \""
                + strVarName
                + "\" }";
            try
            {
                Uri address = new Uri(m_strUrl + "/api/config/get");
                System.Net.HttpWebRequest request =
                    (HttpWebRequest)System.Net.WebRequest.Create(address);

                request.Accept = "text/javascript";
                request.ContentType = "application/json";
                request.Headers.Add("Content-Encoding", "utf-8");

                Encoding asciiEncoding = Encoding.ASCII;
                byte[] byteArray = asciiEncoding.GetBytes(
                    m_strUserName + ":" + m_strPassword);
                request.Headers.Add(
                    HttpRequestHeader.Authorization,
                    "Basic " + Convert.ToBase64String(byteArray));

                request.Method = "POST";
                request.ContentLength = Encoding.UTF8.GetByteCount(
                    strParameter);
                request.GetRequestStream().Write(
                    Encoding.UTF8.GetBytes(strParameter),
                    0,
                    Encoding.UTF8.GetByteCount(strParameter));

                using (System.Net.WebResponse response
                        = request.GetResponse())
                {
                    System.IO.StreamReader sr =
                        new System.IO.StreamReader(
                            response.GetResponseStream());
                    return sr.ReadToEnd();
                }
            }
            catch (Exception ex)
            {
                return "";
            }
        }

        private static string Json_DBUri_Get()
        {
            string strParameter = "{}";
            try
            {
                Uri address = new Uri(m_strUrl + "/api/config/dburi");
                System.Net.HttpWebRequest request =
                    (HttpWebRequest)System.Net.WebRequest.Create(address);

                request.Accept = "text/javascript";
                request.ContentType = "application/json";
                request.Headers.Add("Content-Encoding", "utf-8");

                Encoding asciiEncoding = Encoding.ASCII;
                byte[] byteArray = asciiEncoding.GetBytes(
                    m_strUserName + ":" + m_strPassword);
                request.Headers.Add(
                    HttpRequestHeader.Authorization,
                    "Basic " + Convert.ToBase64String(byteArray));

                request.Method = "POST";
                request.ContentLength = Encoding.UTF8.GetByteCount(
                    strParameter);
                request.GetRequestStream().Write(
                    Encoding.UTF8.GetBytes(strParameter),
                    0,
                    Encoding.UTF8.GetByteCount(strParameter));

                using (System.Net.WebResponse response
                        = request.GetResponse())
                {
                    System.IO.StreamReader sr =
                        new System.IO.StreamReader(
                            response.GetResponseStream());
                    return sr.ReadToEnd();
                }
            }
            catch (Exception ex)
            {
                return "";
            }
        }

    }
}
